package service;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.AppMsgKit;
import common.kit.PageSql;
import enums.StaffStatus;
import enums.WxuserRole;
import model.Staff;
import model.Wxuser;

public class StaffService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " ts.id,ts.createTime,ts.name,tsr.name AS servicerName,ts.mobile,ts.idcard,ts.license,ts.status,ts.refuseReason " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_staff ts ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = ts.servicerId ") ;
		pageSql.setLikeCondition("ts.name") ; //姓名	 
		pageSql.setLikeCondition("ts.mobile") ; //手机号码	 
		pageSql.setLikeCondition("ts.idcard") ; //身份证号	 
		pageSql.setEqualCondition("ts.status") ; //状态	 
		pageSql.setLikeCondition("tsr.name") ; //所属公司	 
	    pageSql.orderBy(" ts.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("statusStr", StaffStatus.getTitleByValue(record , "status"));
		}
	    return page ;
	}

	public Ret delete(Long id) {
        Staff staff = Staff.dao.findById(id);
        if(null == staff){
            return fail();
        }
        staff.delete() ;
		Wxuser.dao.deleteStaff(staff);
        return ok();
    }
	
	public Ret audit(Long id , String reason , boolean isPass) {
		Staff staff = Staff.dao.findById(id);
        if(null == staff){
            return fail();
        }
        if (!staff.isAuditingStatus()) {
			return fail("状态已变更");
		}
        if (isPass) {
        	staff.setNormalStatus();
        	staff.update();
            Wxuser.dao.saveForStaff(staff.getMobile(), WxuserRole.getStaff(), staff.getServicerId());
            //发送审核通知
            AppMsgKit.sendAuditStaffMsg();
		}else {
			staff.setRefusedStatus();
			staff.setRefuseReason(reason);
			staff.update();
			//发送审核通知
            AppMsgKit.sendAuditStaffMsg();
		}
        return ok() ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " ts.id,ts.name,ts.mobile,ts.status,ts.refuseReason " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_staff ts ") ;
		pageSql.setEqualCondition("ts.servicerId") ; //所属公司	 
	    pageSql.orderBy(" ts.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("statusStr", StaffStatus.getTitleByValue(record , "status"));
		}
	    return page ;
	}
	
	public Ret save(Staff staff , Wxuser wxuser) {
		Long servicerId = wxuser.getCompanyId() ;
		Staff ex = Staff.dao.findFirstByIdcardAndServicerId(staff.getIdcard(), servicerId);
		if (null != ex && ex.isNormalStatus()) {
			return fail("该运维人员已添加");
		}
		staff.setServicerId(servicerId);
		staff.setAuditingStatus();
		if (null != ex) {
			staff.setId(ex.getId());
			staff.update();
			return ok() ;
		}
		staff.save();
		return ok() ;
	}
	
	public Ret remove(Long id, Wxuser wxuser) {
		Staff staff = Staff.dao.findById(id);
		if (null == staff || !staff.getServicerId().equals(wxuser.getCompanyId()) ) {
			return fail();
		}
		staff.delete() ;
		Wxuser.dao.deleteStaff(staff);
		return ok() ;
	}
	
	
}
